import Vue from 'vue'
import Vuex from 'vuex'
import vuexPersistedstate from 'vuex-persistedstate'
import { getOperatorMenu } from '../util/internal.api';
import exmod from '../util/exmod';
import router from '../router/index.js';

Vue.use(Vuex)
const store = new Vuex.Store({
	//静态值
	state: {
		reload:false,
		isIE:false, //判断当前浏览器师傅是IE

		authorizations: "", //初始化身份令牌
		operatorInfo: {}, //操作员信息
		menu: [], //操作员菜单数组
		dict: [], //数据字典存储集合
		FingerBase64:"", //指纹的BASE64字符串


		deviceCountPanel:{},
		deviceOffLine:[],  //离线设备列表

		webConfig:{},

	},


	getters: {
		//是否初始化系统配置
		hasInitWebConfig(state){
			return !exmod.isEmptyObject(state.webConfig);

		},

		//操作员是否已经登录
		hasLogin(state) {
			return state.authorizations.length > 0;
		},
		hasRouterInit(state) {
			return state.menu.length > 0;

		},
		//获取菜单数据初始化状态
		menuInitStatus(state) {
			return state.menu.length > 0;

		}


		,
		allowClear(state){
			return !state.isIE;
		},

		hasFinger(state){
			return state.FingerBase64.length>0;
		}


	},



	//同步修改数据
	mutations: {
		setWebConfig(state,data){

			state.webConfig=data;
		},


		//设置离线设备列表
		setDeviceOffLine(state,data){
			state.deviceOffLine=data;
		},
		
		//更新当前设备面板统计数据
		setDeviceCountPanel(state,data){
			state.deviceCountPanel=data;
		},
		//修改当前浏览器信息
		setReload(state,data){
			state.reload=data;
		},


		setFingerBase64(state,data){
			state.FingerBase64=data;
		},

		//修改当前浏览器信息
		setIsIe(state,data){
			state.isIE=data;
		},




		//身份身份令牌
		setAuthorizations(state, data) {
			state.authorizations = data;
		},
		//设置员工登录信息
		setOperatorInfo(state, data) {
			state.operatorInfo = data;
		},
		//设置路由初始化状态
		setRouterInit(state, data) {
			state.routerInit = data;
		},

		//登录事件
		setMenu(state, data) {
			state.menu = data
		},
		//新增字典
		setDict(state, obj) {
			const xzd = state.dict;
			xzd.push(obj);
			state.dict = xzd;
		},

		//删除字典 根据字典代码
		delDict(state, code) {
			var xzd = state.dict;
			//根据字典代码 返回字典的索引
			let index = xzd.findIndex(item => {
				return item.dictionaryCode === code
			})
			if (index >= 0) {
				xzd.splice(index, 1);
				state.dict = xzd;
			}
		}
	},



	//store 一步方法

	actions: {
		//获取操作员路由数据
	  getMenuTree({commit}) {
				//调用AJAX方法重新获取权限
		  getOperatorMenu().then(res => {
					//格式化权限JSON
					let routerObj = exmod.formatMenData(res.data.menu);

					//注册菜单
					router.addRoutes(routerObj);

					//保存格式化后的菜单
					commit('setMenu', routerObj);

				});

		},



	},







	plugins: [vuexPersistedstate({
		key:"_tempData",  //显示的名称
	 	storage: window.sessionStorage,
		 reducer (state) {
			 // 深拷贝一份 删除不想保存的
		      let _state = exmod.deepCopy(state)
			  //删除菜单 不本次存储
		      delete _state.menu
		      return _state
		    }


	 })],

});
export default store;
